<?php
	function toTwitter($twitterId, $senderName, $senderFirstName, $message, $messageTitle, $direct){
                //generates the shorten message for Twitter

		$namestr = $senderFirstName." ".$senderName;
		$smallHint = " Would like to contact you. Please check your email!";

		$namelen = strlen($namestr);    //how long is the name
		$hintlen = strlen(" Would like to contact you on the subject of '', please check your email!");
		$messagelen = strlen($message);
		$smallHintLen = strlen($smallHint);

		if ($direct == true){

			/*
			#=================================================#
			#     We're going to send a tweet as mention.     #
			#=================================================#
			*/

			if ( $namelen + $messagelen + 7 <= 140){	//Send complete message

				$shortmessage = $namestr." says: ".$message;

			} else if ( $namelen + $hintlen + 10 <= 140 ){	//send hint with theme if at least 10 characters of the title fit

				//calculate how long the title can be
				$maxTitleLen = 140 - $namelen - $hintlen;
				$titleSubStr = substr($messageTitle, 0, $maxTitleLen );
				$shortmessage = $namestr." Would like to contact you on the subject of '".$titleSubStr."', please check your email!";

			} else if ( $namelen + $smallHintLen <= 140) {	//send hint without theme

				$shortmessage = $namestr.$smallHint;

			} else {

				$initials = substr($senderFirstName, 0, 1).".".substr($senderName,0 ,1).".";
				$shortmessage = $initials.$smallHint;

			}

		} else {

			/*
			#=================================================#
			#      We're going to send a tweet as mention     #
			#=================================================#
			*/

			$twitterIdLen = strlen($twitterId);
			//$smallHintStrLen = strlen(" Would like to contact you. Please check your email!");

			If ( $twitterIdLen + +$namelen + $messagelen + 9 <= 140){	//send complete message

				$shortmessage = "@".$twitterId." ".$namestr." says: ".$message;

			} else if ( $twitterIdLen + $namelen + $hintlen + 12 <= 140 ){

				//calculate how long the title can be
                                $maxTitleLen = 140 - $twitterIdLen - $namelen - $hintlen -2;
				$titleSubStr = substr($messageTitle, 0, $maxTitleLen);
				$shortmessage = "@".$twitterId." ".$namestr." Would like to contact you on the subject of '".$titleSubStr."', please check your email!";

			} else if ( $twitterIdLen + $namelen + $smallHintLen + 2 <= 140){

				$shortmessage = "@".$twitterId." ".$namestr.$smallHint;

			} else {

				$initials = substr($senderFirstName, 0, 1).".".substr($senderName,0 ,1).".";
				$shortmessage = "@".$twitterId." ".$initials.$smallHint;

			}

		}

                return($shortmessage);
	}
?>
